import 'package:flutter/cupertino.dart' show CupertinoIcons;
import 'package:flutter/material.dart';

import '../../../config.dart';
import '../../resources/resources.dart';
import '../my_text.dart';

//typedef MyItemTagTapCallback = void Function(dynamic tag);

enum MyItemType {
  next, //进入下一步
  options, //选项
  unfold, //展开
  sw, //开关
  cupSw, //Cupertino开关
  none, //空的
}

enum DividingLineType {
  solid,
  dotted,
}

class MyItem extends StatelessWidget {
  const MyItem({
    super.key,
    this.onTap,
    //this.onTagTap,
    required this.title,
    this.tag,
    this.isOn = false,
    this.leftWidget,
    this.titleWidget,
    this.trailingWidget,
    this.subtitle,
    this.trailingText,
    this.type = MyItemType.next,
    this.dividingLine = true,
    this.i18n = true,
    this.dividingLineType = DividingLineType.solid,
    this.height = 60,
    this.leftPadding = 16,
    this.color,
    this.padding,
    this.trailingTextColor,
    this.trailingTextSize = 16,
    this.trailingIcon,
    this.subtitleI18n = false,
    this.trailingI18n = false,
    this.titleColor,
    this.switchActiveColor,
    this.titleFontWeight,
  });

  final VoidCallback? onTap;
  final String title;
  final String? subtitle;
  final String? trailingText;
  final dynamic tag;
  final bool isOn;
  final Widget? leftWidget;
  final Widget? trailingWidget;
  final Widget? trailingIcon;
  final Widget? titleWidget;
  final MyItemType type;
  final bool dividingLine;
  final bool i18n;
  final bool subtitleI18n;
  final bool trailingI18n;
  final DividingLineType dividingLineType;
  final double height;
  final double leftPadding;
  final Color? color;
  final Color? titleColor;
  final Color? switchActiveColor;
  final FontWeight? titleFontWeight;

  final EdgeInsets? padding;

  final Color? trailingTextColor;
  final double trailingTextSize;

  Widget trailing() {
    if (type == MyItemType.none && trailingIcon == null) {
      return Container();
    }

    return Padding(
      padding: const EdgeInsets.only(left: 16.0),
      child: Builder(builder: (context) {
        if (trailingIcon != null) {
          return trailingIcon!;
        }
        switch (type) {
          case MyItemType.next:
            return Icon(
              CupertinoIcons.right_chevron,
              color: JmConfig.sbuTextStyle.color,
              size: 18,
            );
          case MyItemType.options:
            if (isOn) {
              return Icon(CupertinoIcons.check_mark_circled_solid,
                  color: JmColors.main);
            } else {
              return const SizedBox(
                width: 24,
                height: 24,
              );
            }
          case MyItemType.unfold:
            return Icon(
              Icons.unfold_more,
              color: Colors.grey[400],
            );
          case MyItemType.sw:
            return Switch(
              value: isOn,
              activeColor: switchActiveColor ?? JmColors.main,
              onChanged: (_) {
                if (onTap != null) {
                  onTap!();
                }
              },
            );
          case MyItemType.cupSw:
            return CupSwitch(
              value: isOn,
              activeColor: switchActiveColor ?? JmColors.main,
              onChanged: (_) {
                if (onTap != null) {
                  onTap!();
                }
              },
            );
          case MyItemType.none:
            return Container();
        }
      }),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      color: color ?? Colors.transparent,
      child: InkWell(
        //animation: MyInkWellAnimation.ink,
        onTap: onTap != null
            ? () {
                if (onTap != null) {
                  onTap!();
                }
              }
            : null,
        child: Container(
          margin:
              EdgeInsets.only(top: 0, left: leftPadding, bottom: 0, right: 0),
          constraints:
              BoxConstraints(maxHeight: double.infinity, minHeight: height),
          width: double.infinity,
          child: Row(
            children: <Widget>[
              leftWidget != null
                  ? Container(
                      padding: const EdgeInsets.only(right: 10),
                      child: leftWidget,
                    )
                  : Container(),
              Expanded(
                child: Container(
                  constraints: BoxConstraints(
                      maxHeight: double.infinity, minHeight: height),
                  decoration: BoxDecoration(
                    border: dividingLine
                        ? Border(
                            bottom: Divider.createBorderSide(context,
                                width: 0.6, color: JmColors.div),
                          )
                        : null,
                  ),
                  child: Row(
                    children: [
                      Expanded(
                        child: subtitle != null
                            ? Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: [
                                  titleWidget ??
                                      MyText(
                                        title,
                                        color: titleColor,
                                        //size: 16,
                                        weight: titleFontWeight,
                                        textAlign: TextAlign.start,
                                        i18n: i18n,
                                      ),
                                  const SizedBox(height: 1.0),
                                  MyText(
                                    subtitle!,
                                    size: 14,
                                    i18n: subtitleI18n,
                                    color: JmConfig.sbuTextStyle.color,
                                    textAlign: TextAlign.start,
                                  ),
                                ],
                              )
                            : titleWidget ??
                                MyText(
                                  title,
                                  size: 16,
                                  color: titleColor,
                                  //color: Color(0xFF666666),
                                  textAlign: TextAlign.start,
                                  weight: titleFontWeight,
                                  i18n: i18n,
                                ),
                      ),
                      const SizedBox(width: 10.0),
                      trailingText != null
                          ? ConstrainedBox(
                              constraints: BoxConstraints(maxWidth: 200),
                              child: MyText(
                                trailingText!,
                                size: trailingTextSize,
                                color: trailingTextColor ??
                                    JmConfig.sbuTextStyle.color,
                                textAlign: TextAlign.start,
                                i18n: trailingI18n,
                              ),
                            )
                          : Container(),
                      //Spacer(),
                      trailingWidget ?? Container(),
                      trailing(),
                      gaph16,
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
